<!DOCTYPE html>
<html >
<head>

    <!--[if lt IE 9]>
        <style>body {display: none; background: none !important} </style>
        <meta http-equiv="Refresh" Content="0; url=//outdatedbrowser.com/" />
    <![endif]-->

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no" />
<meta name="author" content="Mark" />



<meta name="description" content="matlab中集成许多易用的算法，这些算法不仅仅能运行在matlab环境下，经过编译我们写好的matlab脚本甚至可以运行于C++环境。这可以大大缩短开发周期，但可能一定程度降低程序性能（相对于纯C++程序而言）。">
<meta property="og:type" content="article">
<meta property="og:title" content="matlab cpp混编">
<meta property="og:url" content="http://markma.tk/posts/Matlab/matlab cpp混编/index.html">
<meta property="og:site_name" content="Mark's Blog">
<meta property="og:description" content="matlab中集成许多易用的算法，这些算法不仅仅能运行在matlab环境下，经过编译我们写好的matlab脚本甚至可以运行于C++环境。这可以大大缩短开发周期，但可能一定程度降低程序性能（相对于纯C++程序而言）。">
<meta property="og:image" content="http://i.imgur.com/oXs9CwX.png">
<meta property="og:image" content="http://i.imgur.com/byopBgA.png">
<meta property="og:image" content="http://i.imgur.com/eW2gp6h.png">
<meta property="og:image" content="http://i.imgur.com/4jW74OI.png">
<meta property="og:image" content="http://i.imgur.com/5YC8N4u.png">
<meta property="og:image" content="http://i.imgur.com/lNv2rQO.png">
<meta property="og:image" content="http://i.imgur.com/jg92xMZ.png">
<meta property="og:updated_time" content="2017-05-06T15:20:48.359Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="matlab cpp混编">
<meta name="twitter:description" content="matlab中集成许多易用的算法，这些算法不仅仅能运行在matlab环境下，经过编译我们写好的matlab脚本甚至可以运行于C++环境。这可以大大缩短开发周期，但可能一定程度降低程序性能（相对于纯C++程序而言）。">
<meta name="twitter:image" content="http://i.imgur.com/oXs9CwX.png">

<link rel="apple-touch-icon" href= "/apple-touch-icon.png">




    <link rel="shortcut icon" href="/favicon.png">



    <link href="//cdn.bootcss.com/animate.css/3.5.1/animate.min.css" rel="stylesheet">



    <link href="//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet">



    <script src="//cdn.bootcss.com/pace/1.0.2/pace.min.js"></script>
    <link href="//cdn.bootcss.com/pace/1.0.2/themes/blue/pace-theme-minimal.css" rel="stylesheet">


<link rel="stylesheet" href="/css/style.css">



<link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">


<title>matlab cpp混编 | Mark&#39;s Blog</title>

<script src="//cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>

<script>
    var yiliaConfig = {
        fancybox: true,
        animate: true,
        isHome: false,
        isPost: true,
        isArchive: false,
        isTag: false,
        isCategory: false,
        fancybox_js: "//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.js",
        scrollreveal: "//cdn.bootcss.com/scrollReveal.js/3.1.4/scrollreveal.min.js",
        search: true
    }
</script>


    <script>
        yiliaConfig.jquery_ui = [true, "//cdn.bootcss.com/jqueryui/1.10.4/jquery-ui.min.js", "//cdn.bootcss.com/jqueryui/1.10.4/css/jquery-ui.min.css"];
    </script>



    <script> yiliaConfig.rootUrl = "\/";</script>





    <script>
        var _hmt = _hmt || [];
        (function() {
            var hm = document.createElement("script");
            hm.src = "//hm.baidu.com/hm.js?fad5c01287c961d95e4fe533d91c8aab";
            var s = document.getElementsByTagName("script")[0]; 
            s.parentNode.insertBefore(hm, s);
        })();
    </script>


</head>
<body>
  <div id="container">
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
    <header id="header" class="inner">
        <a href="/" class="profilepic">
            <img src="/img/avatar.png" class="animated zoomIn">
        </a>
        <hgroup>
          <h1 class="header-author"><a href="/">Mark</a></h1>
        </hgroup>

        
        <p class="header-subtitle">To do to make progress</p>
        

        
            <form id="search-form">
            <input type="text" id="local-search-input" name="q" placeholder="search..." class="search form-control" autocomplete="off" autocorrect="off" searchonload="false" />
            <i class="fa fa-times" onclick="resetSearch()"></i>
            </form>
            <div id="local-search-result"></div>
            <p class='no-result'>No results found <i class='fa fa-spinner fa-pulse'></i></p>
        


        
            <div id="switch-btn" class="switch-btn">
                <div class="icon">
                    <div class="icon-ctn">
                        <div class="icon-wrap icon-house" data-idx="0">
                            <div class="birdhouse"></div>
                            <div class="birdhouse_holes"></div>
                        </div>
                        <div class="icon-wrap icon-ribbon hide" data-idx="1">
                            <div class="ribbon"></div>
                        </div>
                        
                        <div class="icon-wrap icon-link hide" data-idx="2">
                            <div class="loopback_l"></div>
                            <div class="loopback_r"></div>
                        </div>
                        
                        
                        <div class="icon-wrap icon-me hide" data-idx="3">
                            <div class="user"></div>
                            <div class="shoulder"></div>
                        </div>
                        
                    </div>
                    
                </div>
                <div class="tips-box hide">
                    <div class="tips-arrow"></div>
                    <ul class="tips-inner">
                        <li>Menu</li>
                        <li>Tags</li>
                        
                        <li>Friends</li>
                        
                        
                        <li>About Me</li>
                        
                    </ul>
                </div>
            </div>
        

        <div id="switch-area" class="switch-area">
            <div class="switch-wrap">
                <section class="switch-part switch-part1">
                    <nav class="header-menu">
                        <ul>
                        
                            <li><a href="/">主页</a></li>
                        
                            <li><a href="/tags/">分类</a></li>
                        
                            <li><a href="/archives/">归档</a></li>
                        
                            <li><a href="/gallery/">相册</a></li>
                        
                            <li><a href="/about/">关于我</a></li>
                        
                        </ul>
                    </nav>
                    <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" href="mailto:ma-mark@outlook.com" title="Email"></a>
                            
                                <a class="fa 新浪微博" href="http://weibo.com/mark900" title="新浪微博"></a>
                            
                                <a class="fa GitHub" href="https://github.com/markma" title="GitHub"></a>
                            
                                <a class="fa 知乎" href="https://www.zhihu.com/people/mark900" title="知乎"></a>
                            
                                <a class="fa Coding" href="https://coding.net/u/maxiaohang" title="Coding"></a>
                            
                                <a class="fa 网易云音乐" href="http://music.163.com/#/user/home?id=115229883" title="网易云音乐"></a>
                            
                                <a class="fa QQ" href="http://wpa.qq.com/msgrd?v=3&uin=954373276&site=qq&menu=yes" title="QQ"></a>
                            
                        </ul>
                    </nav>
                </section>
                
                
                <section class="switch-part switch-part2">
                    <div class="widget tagcloud" id="js-tagcloud">
                        <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Cpp/">Cpp</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Hexo/">Hexo</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Journal/">Journal</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Matlab/">Matlab</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/OpenLibrary/">OpenLibrary</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Python/">Python</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Qt/">Qt</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Robot/">Robot</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Wiki/">Wiki</a></li></ul>
                    </div>
                </section>
                
                
                
                <section class="switch-part switch-part3">
                    <div id="js-friends">
                    
                      <a class="main-nav-link switch-friends-link" href="http://moxfive.xyz/">MOxFIVE</a>
                    
                    </div>
                </section>
                

                
                
                <section class="switch-part switch-part4">
                
                    <div id="js-aboutme">因为爱好，做着自己本不该做的事。为爱而活，不受羁绊！</div>
                </section>
                
            </div>
        </div>
    </header>                
</div>
    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
      <div class="overlay">
          <div class="slider-trigger"></div>
          <h1 class="header-author js-mobile-header hide"><a href="/" title="回到主页">Mark</a></h1>
      </div>
    <div class="intrude-less">
        <header id="header" class="inner">
            <a href="/" class="profilepic">
                <img src="/img/avatar.png" class="animated zoomIn">
            </a>
            <hgroup>
              <h1 class="header-author"><a href="/" title="回到主页">Mark</a></h1>
            </hgroup>
            
            <p class="header-subtitle">To do to make progress</p>
            
            <nav class="header-menu">
                <ul>
                
                    <li><a href="/">主页</a></li>
                
                    <li><a href="/tags/">分类</a></li>
                
                    <li><a href="/archives/">归档</a></li>
                
                    <li><a href="/gallery/">相册</a></li>
                
                    <li><a href="/about/">关于我</a></li>
                
                <div class="clearfix"></div>
                </ul>
            </nav>
            <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" target="_blank" href="mailto:ma-mark@outlook.com" title="Email"></a>
                            
                                <a class="fa 新浪微博" target="_blank" href="http://weibo.com/mark900" title="新浪微博"></a>
                            
                                <a class="fa GitHub" target="_blank" href="https://github.com/markma" title="GitHub"></a>
                            
                                <a class="fa 知乎" target="_blank" href="https://www.zhihu.com/people/mark900" title="知乎"></a>
                            
                                <a class="fa Coding" target="_blank" href="https://coding.net/u/maxiaohang" title="Coding"></a>
                            
                                <a class="fa 网易云音乐" target="_blank" href="http://music.163.com/#/user/home?id=115229883" title="网易云音乐"></a>
                            
                                <a class="fa QQ" target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=954373276&site=qq&menu=yes" title="QQ"></a>
                            
                        </ul>
            </nav>
        </header>                
    </div>
    <link class="menu-list" tags="Tags" friends="Friends" about="About Me"/>
</nav>
      <div class="body-wrap"><article id="post-posts/Matlab/matlab cpp混编" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="/posts/Matlab/matlab cpp混编/" class="article-date">
      <time datetime="2017-05-06T10:30:05.000Z" itemprop="datePublished">2017-05-06</time>
</a>


    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      matlab cpp混编
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        
    <div class="article-category tagcloud">
    <a class="article-category-link" href="/categories/OpenSource/">OpenSource</a>
    </div>


        
    <div class="article-tag tagcloud">
        <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Cpp/">Cpp</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Matlab/">Matlab</a></li></ul>
    </div>

        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
          
        <p>matlab中集成许多易用的算法，这些算法不仅仅能运行在matlab环境下，经过编译我们写好的matlab脚本甚至可以运行于C++环境。这可以大大缩短开发周期，但可能一定程度降低程序性能（相对于纯C++程序而言）。<br><a id="more"></a></p>
<h2 id="环境配置"><a href="#环境配置" class="headerlink" title="环境配置"></a>环境配置</h2><p>首先确保装有C++编译器，matlab。并且要保证C++编译器与matlab有着相同的位数（都为X64或X86）。</p>
<h3 id="配置matlab-C-编译环境"><a href="#配置matlab-C-编译环境" class="headerlink" title="配置matlab C++编译环境"></a>配置matlab C++编译环境</h3><p>在matlab中输入<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&gt;&gt;mex -setup</div></pre></td></tr></table></figure></p>
<p>配置C语言编译器<br><img src="http://i.imgur.com/oXs9CwX.png" alt=""><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&gt;&gt;mbuild -setup</div></pre></td></tr></table></figure></p>
<p><img src="http://i.imgur.com/byopBgA.png" alt=""><br>到此matlab c语言编译环境设置完成。</p>
<h2 id="使用实例"><a href="#使用实例" class="headerlink" title="使用实例"></a>使用实例</h2><p>配置好环境后，下面以一个绘图实例详细讲解如何生成和使用matlab脚本的动态链接库。</p>
<figure class="highlight matlab"><figcaption><span>mplot.m</span></figcaption><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span><span class="params">[]</span> =<span class="title">mplot</span><span class="params">(x,y)</span></span></div><div class="line">plot(x,y)</div></pre></td></tr></table></figure>
<h3 id="生成动态链接库"><a href="#生成动态链接库" class="headerlink" title="生成动态链接库"></a>生成动态链接库</h3><h4 id="方法1"><a href="#方法1" class="headerlink" title="方法1"></a>方法1</h4><p>命令行输入 <code>&gt;&gt; deploytool</code> 打开开发工具,-&gt;Library Compiler选择C++ shared library和对应的matlab脚本文件。<br><img src="http://i.imgur.com/eW2gp6h.png" alt=""><br><img src="http://i.imgur.com/4jW74OI.png" alt=""><br>编译完成后即生成下面的文件夹。<br><img src="http://i.imgur.com/5YC8N4u.png" alt=""><br>下面的文件即为cpp程序中即将用到的动态链接库。<br><img src="http://i.imgur.com/lNv2rQO.png" alt=""></p>
<h4 id="方法2"><a href="#方法2" class="headerlink" title="方法2"></a>方法2</h4><p>命令行执行<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mcc -l mplot.m</div></pre></td></tr></table></figure></p>
<p>同样可以生成动态链接库。</p>
<h3 id="动态链接库调用"><a href="#动态链接库调用" class="headerlink" title="动态链接库调用"></a>动态链接库调用</h3><p>如果调用不成功，请检查运行环境，比如编译器是否为win32,或者win64，要与matlab一致。 其次是否安装matlab runtime（matlab中命令行执行<code>&gt;&gt;mcrinstaller</code>）。</p>
<p>运行环境搞定后，需要将matlab中的运行库提取出来，或者直接包含到cpp工程中。</p>
<p>下面我们使用qt creator IDE为例介绍其使用。</p>
<h4 id="提取并配置matlab-VC运行库"><a href="#提取并配置matlab-VC运行库" class="headerlink" title="提取并配置matlab VC运行库"></a>提取并配置matlab VC运行库</h4><p>在matlab安装目录下\Matlab2015_x64\extern文件夹内拷贝inlcude和lib（或者使用windows的环境变量包含上述头文件）。<br>在QT工程配置文件中包含拷贝出来的include目录，并添加lib文件夹下的链接库。可以通过配置.pri文件实现<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div></pre></td><td class="code"><pre><div class="line">LIBS += -L$$PWD \</div><div class="line">-lgpu\</div><div class="line">-llibcovrt\</div><div class="line">-llibemlrt\</div><div class="line">-llibeng\</div><div class="line">-llibfixedpoint\</div><div class="line">-llibmat\</div><div class="line">-llibmex\</div><div class="line">-llibmwblas\</div><div class="line">-llibmwblascompat32\</div><div class="line">-llibmwboxfilter\</div><div class="line">-llibmwboxfilter3\</div><div class="line">-llibmwbwdistEDT\</div><div class="line">-llibmwbwdistEDT_tbb\</div><div class="line">-llibmwbwdistEDTFT\</div><div class="line">-llibmwbwdistEDTFT_tbb\</div><div class="line">-llibmwbwlookup\</div><div class="line">-llibmwbwlookup_tbb\</div><div class="line">-llibmwbwpackc\</div><div class="line">-llibmwbwpackctbb\</div><div class="line">-llibmwbwunpackc\</div><div class="line">-llibmwbwunpackctbb\</div><div class="line">-llibmwcannythresholding_tbb\</div><div class="line">-llibmwcascadeClassifier\</div><div class="line">-llibmwcgir_construct\</div><div class="line">-llibmwcgxert\</div><div class="line">-llibmwcoder_connectivity_comm\</div><div class="line">-llibmwcoder_connectivity_memunit\</div><div class="line">-llibmwcoder_ParamTuningTgtAppSvc\</div><div class="line">-llibmwcoder_target_services\</div><div class="line">-llibmwcoder_ToAsyncQueueTgtAppSvc\</div><div class="line">-llibmwcoderassumptionsapp\</div><div class="line">-llibmwcoderprofile\</div><div class="line">-llibmwComputeMetric\</div><div class="line">-llibmwddist\</div><div class="line">-llibmwdetectBRISK\</div><div class="line">-llibmwdetectFAST\</div><div class="line">-llibmwdetectMser\</div><div class="line">-llibmwdisparityBM\</div><div class="line">-llibmwdisparitySGBM\</div><div class="line">-llibmwedgethinning_tbb\</div><div class="line">-llibmwextractBRISK\</div><div class="line">-llibmwextractFreak\</div><div class="line">-llibmwextractSurf\</div><div class="line">-llibmwfastHessianDetector\</div><div class="line">-llibmwfilcommon\</div><div class="line">-llibmwforegroundDetector\</div><div class="line">-llibmwgetnumcores\</div><div class="line">-llibmwgrayto16\</div><div class="line">-llibmwgrayto8\</div><div class="line">-llibmwgrayxform\</div><div class="line">-llibmwgrayxform_tbb\</div><div class="line">-llibmwHOGDescriptor\</div><div class="line">-llibmwimfilter\</div><div class="line">-llibmwimlincomb\</div><div class="line">-llibmwimlincomb_tbb\</div><div class="line">-llibmwimreconstruct\</div><div class="line">-llibmwimregionalmax\</div><div class="line">-llibmwint16touint16\</div><div class="line">-llibmwint32touint32\</div><div class="line">-llibmwint8touint8\</div><div class="line">-llibmwintlut\</div><div class="line">-llibmwintluttbb\</div><div class="line">-llibmwippfilter\</div><div class="line">-llibmwippgeotrans\</div><div class="line">-llibmwippmedianfilter\</div><div class="line">-llibmwippreconstruct\</div><div class="line">-llibmwlapack\</div><div class="line">-llibmwmatchFeatures\</div><div class="line">-llibmwmathutil\</div><div class="line">-llibmwmorphop_binary\</div><div class="line">-llibmwmorphop_binary_tbb\</div><div class="line">-llibmwmorphop_flat\</div><div class="line">-llibmwmorphop_flat_tbb\</div><div class="line">-llibmwmorphop_ipp\</div><div class="line">-llibmwmorphop_nonflat\</div><div class="line">-llibmwmorphop_nonflat_tbb\</div><div class="line">-llibmwmorphop_packed\</div><div class="line">-llibmwocvgpumex\</div><div class="line">-llibmwocvmex\</div><div class="line">-llibmwopticalFlowFarneback\</div><div class="line">-llibmwopticalFlowHSCore\</div><div class="line">-llibmwopticalFlowLKCore\</div><div class="line">-llibmwopticalFlowLKDoGCore\</div><div class="line">-llibmwordfilt2\</div><div class="line">-llibmwpointTracker\</div><div class="line">-llibmwremap\</div><div class="line">-llibmwremaptbb\</div><div class="line">-llibmwrtiostreamutils\</div><div class="line">-llibmwservices\</div><div class="line">-llibmwsigstream\</div><div class="line">-llibmwsimulink\</div><div class="line">-llibmwsl_AsyncioQueue\</div><div class="line">-llibmwsl_fileio\</div><div class="line">-llibmwsl_iofile\</div><div class="line">-llibmwsl_log_load_blocks\</div><div class="line">-llibmwsl_sfcn_cov_bridge\</div><div class="line">-llibmwsl_solver_rtw\</div><div class="line">-llibmwslexec_parallel\</div><div class="line">-llibmwslexec_simbridge\</div><div class="line">-llibmwtbbhist\</div><div class="line">-llibmwuint16toint16\</div><div class="line">-llibmwxilcomms\</div><div class="line">-llibmwxilcomms_rtiostream\</div><div class="line">-llibmwxilservice\</div><div class="line">-llibmwxilutils\</div><div class="line">-llibmwxilutils_sl\</div><div class="line">-llibmx\</div><div class="line">-llibut\</div><div class="line">-lmclbase\</div><div class="line">-lmclcommain\</div><div class="line">-lmclmcr\</div><div class="line">-lmclmcrrt\</div><div class="line">-lmclxlmain\</div><div class="line">-lne_mli\</div><div class="line">-lne_rtl\</div><div class="line">-locrutils\</div><div class="line">-lphysmod_common_external_library_kernel\</div><div class="line">-lphysmod_common_foundation_core_util\</div><div class="line">-lrtwcg\</div><div class="line">-lsf_runtime\</div><div class="line">-lSimulinkBlock\</div><div class="line">-lsl_services</div></pre></td></tr></table></figure></p>
<h4 id="配置mplot-VC运行库"><a href="#配置mplot-VC运行库" class="headerlink" title="配置mplot VC运行库"></a>配置mplot VC运行库</h4><p>同样额mplot的动态链接库也要添加进工程。</p>
<h4 id="调用"><a href="#调用" class="headerlink" title="调用"></a>调用</h4><p>在main函数中添加下属语句，对链接库初始化<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"></div><div class="line"><span class="comment">// 初始化程序</span></div><div class="line"><span class="keyword">if</span>( ! mclInitializeApplication(<span class="literal">NULL</span>,<span class="number">0</span>) )</div><div class="line">&#123;</div><div class="line"><span class="built_in">cout</span> &lt;&lt; <span class="string">"Could not initialize the application.n"</span>&lt;&lt; <span class="built_in">endl</span>;</div><div class="line"><span class="built_in">fprintf</span>(<span class="built_in">stderr</span>, <span class="string">"Could not initialize the application.n"</span>);</div><div class="line"><span class="built_in">exit</span>(<span class="number">1</span>);</div><div class="line">&#125;</div><div class="line"><span class="comment">// 初始化库</span></div><div class="line"><span class="keyword">if</span> (! mplotInitialize() )</div><div class="line">&#123;</div><div class="line"><span class="built_in">cout</span> &lt;&lt;  <span class="string">"Could not initialize the library.n"</span>&lt;&lt; <span class="built_in">endl</span>;</div><div class="line"><span class="built_in">fprintf</span>(<span class="built_in">stderr</span>,<span class="string">"Could not initialize the library.n"</span>);</div><div class="line"><span class="built_in">exit</span>(<span class="number">1</span>);</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>matlab中数据都以矩阵的形式来表示，即便是在cpp中也是如此。matlab提供了Cpp数据接口mwArray，具体使用方法见<a href="http://cn.mathworks.com/help/compiler_sdk/cxx/mwarray.html?searchHighlight=mwArray&amp;s_tid=doc_srchtitle" target="_blank" rel="external">官方说明</a>。下面仅以一个简单例子进行介绍。<br>首先将所需的数据保存值mxDouble数组，然后实例化mwArray，并将数据链接到mwArray。如下：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">mxDouble x[]=&#123;</div><div class="line">    <span class="number">0</span>,<span class="number">0.157079632679490</span>,<span class="number">0.314159265358979</span>,<span class="number">0.471238898038469</span>,<span class="number">0.628318530717959</span>,<span class="number">0.785398163397448</span>,<span class="number">0.942477796076938</span>,<span class="number">1.09955742875643</span>,<span class="number">1.25663706143592</span>,<span class="number">1.41371669411541</span>,<span class="number">1.57079632679490</span>,<span class="number">1.72787595947439</span>,<span class="number">1.88495559215388</span>,<span class="number">2.04203522483337</span>,<span class="number">2.19911485751286</span>,<span class="number">2.35619449019235</span>,<span class="number">2.51327412287183</span>,<span class="number">2.67035375555132</span>,<span class="number">2.82743338823081</span>,<span class="number">2.98451302091030</span>,<span class="number">3.14159265358979</span>,<span class="number">3.29867228626928</span>,<span class="number">3.45575191894877</span>,<span class="number">3.61283155162826</span>,<span class="number">3.76991118430775</span>,<span class="number">3.92699081698724</span>,<span class="number">4.08407044966673</span>,<span class="number">4.24115008234622</span>,<span class="number">4.39822971502571</span>,<span class="number">4.55530934770520</span>,<span class="number">4.71238898038469</span>,<span class="number">4.86946861306418</span>,<span class="number">5.02654824574367</span>,<span class="number">5.18362787842316</span>,<span class="number">5.34070751110265</span>,<span class="number">5.49778714378214</span>,<span class="number">5.65486677646163</span>,<span class="number">5.81194640914112</span>,<span class="number">5.96902604182061</span>,<span class="number">6.12610567450010</span>,<span class="number">6.28318530717959</span></div><div class="line">&#125;;</div><div class="line">mxDouble y[]=&#123;</div><div class="line">    <span class="number">2</span>,<span class="number">-0.725324972339547</span>,<span class="number">-1.18317568280710</span>,<span class="number">1.47807570185109</span>,<span class="number">-0.0609006857112387</span>,<span class="number">-1.21904391609666</span>,<span class="number">0.936250913105442</span>,<span class="number">0.362752647593856</span>,<span class="number">-1.06326728866211</span>,<span class="number">0.461192845316532</span>,<span class="number">0.574191813840279</span>,<span class="number">-0.810627669650712</span>,<span class="number">0.0972434975400799</span>,<span class="number">0.622258451229734</span>,<span class="number">-0.534447387607577</span>,<span class="number">-0.144156436564858</span>,<span class="number">0.561318240466811</span>,<span class="number">-0.283936857042294</span>,<span class="number">-0.273776282303907</span>,<span class="number">0.441239433904076</span>,<span class="number">-0.0860629136203344</span>,<span class="number">-0.314675524019288</span>,<span class="number">0.301794479927175</span>,<span class="number">0.0500339580738860</span>,<span class="number">-0.294215306147470</span>,<span class="number">0.170640887043219</span>,<span class="number">0.127676895460899</span>,<span class="number">-0.238443085663152</span>,<span class="number">0.0638314533921737</span>,<span class="number">0.157483497590837</span>,<span class="number">-0.168779371481232</span>,<span class="number">-0.0121711733625802</span>,<span class="number">0.153072982764870</span>,<span class="number">-0.100625734082266</span>,<span class="number">-0.0578353237787552</span>,<span class="number">0.127947779407999</span>,<span class="number">-0.0433755698360067</span>,<span class="number">-0.0778873943022785</span>,<span class="number">0.0935639955835359</span>,<span class="number">-0.00129893145763564</span>,<span class="number">-0.0790210114267234</span></div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="function">mwArray <span class="title">xx</span><span class="params">(<span class="number">41</span>,<span class="number">1</span>,mxDOUBLE_CLASS)</span></span>;</div><div class="line"><span class="function">mwArray <span class="title">yy</span><span class="params">(<span class="number">41</span>,<span class="number">1</span>,mxDOUBLE_CLASS)</span></span>;</div><div class="line">xx.SetData(x,<span class="number">41</span>);</div><div class="line">yy.SetData(y,<span class="number">41</span>);</div></pre></td></tr></table></figure>
<p>最后调用mplot<br>    mplot(xx,yy);</p>
<p>得到绘图结果：<br><img src="http://i.imgur.com/jg92xMZ.png" alt=""></p>

      
    </div>
    
  </div>
  
    
    <div class="copyright">
        <p><span>Title:</span><a href="/posts/Matlab/matlab cpp混编/">matlab cpp混编</a></p>
        <p><span>Author:</span><a href="/" title="Back to Homepage">Mark</a></p>
        <p><span>Created:</span>2017-05-06, 18:30:05</p>
        <p><span>Updated:</span>2017-05-06, 23:20:48</p>
        <p>
            <span>Full URL:</span><a class="post-url" href="/posts/Matlab/matlab cpp混编/" title="matlab cpp混编">http://markma.tk/posts/Matlab/matlab cpp混编/</a>
            <span class="copy-path" data-clipboard-text="From http://markma.tk/posts/Matlab/matlab cpp混编/　　By Mark" title="Copy Article&#39;s Link &amp; Author"><i class="fa fa-clipboard"></i></span>
            <script> var clipboard = new Clipboard('.copy-path'); </script>
        </p>
        <p>
            <span>License:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" title="CC BY-NC-SA 4.0 International" target = "_blank">"CC BY-NC-SA 4.0"</a> Keep Link &amp; Author if Distribute.
        </p>
    </div>



    <nav id="article-nav">
        
        
            <div id="article-nav-older" class="article-nav-title">
                <a href="/posts/QtStudy/Cpp中回调函数与静态成员/">
                    Cpp中回调函数与静态成员
                </a>
            </div>
        
    </nav>

  
</article>

    <div id="toc" class="toc-article">
        <strong class="toc-title">Contents</strong>
        
            <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#环境配置"><span class="toc-number">1.</span> <span class="toc-text">环境配置</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#配置matlab-C-编译环境"><span class="toc-number">1.1.</span> <span class="toc-text">配置matlab C++编译环境</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#使用实例"><span class="toc-number">2.</span> <span class="toc-text">使用实例</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#生成动态链接库"><span class="toc-number">2.1.</span> <span class="toc-text">生成动态链接库</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#方法1"><span class="toc-number">2.1.1.</span> <span class="toc-text">方法1</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#方法2"><span class="toc-number">2.1.2.</span> <span class="toc-text">方法2</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#动态链接库调用"><span class="toc-number">2.2.</span> <span class="toc-text">动态链接库调用</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#提取并配置matlab-VC运行库"><span class="toc-number">2.2.1.</span> <span class="toc-text">提取并配置matlab VC运行库</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#配置mplot-VC运行库"><span class="toc-number">2.2.2.</span> <span class="toc-text">配置mplot VC运行库</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#调用"><span class="toc-number">2.2.3.</span> <span class="toc-text">调用</span></a></li></ol></li></ol></li></ol>
        
    </div>
    <style>
        .left-col .switch-btn,
        .left-col .switch-area {
            display: none;
        }
        .toc-level-3 i,
        .toc-level-3 ol {
            display: none !important;
        }
    </style>

    <input type="button" id="tocButton" value="Hide"  title="Show or Hide Table of Contents">

    <script>
        yiliaConfig.toc = ["Hide", "Show", !!"false"];
    </script>



    
<div class="share">
    
        <div class="bdsharebuttonbox">
            <a href="#" class="fa fa-twitter bds_twi" data-cmd="twi" title="分享到推特"></a>
            <a href="#" class="fa fa-weibo bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
            <a href="#" class="fa fa-qq bds_sqq" data-cmd="sqq" title="分享给 QQ 好友"></a>
            <a href="#" class="fa fa-files-o bds_copy" data-cmd="copy" title="复制网址"></a>
            <a href="#" class="fa fa fa-envelope-o bds_mail" data-cmd="mail" title="通过邮件分享"></a>
            <a href="#" class="fa fa-weixin bds_weixin" data-cmd="weixin" title="生成文章二维码"></a>
            <a href="#" class="fa fa-share-alt bds_more" data-cmd="more"></i></a>
        </div>
        <script>
            window._bd_share_config={
                "common":{"bdSnsKey":{},"bdText":"matlab cpp混编　| Mark's Blog　","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"24"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
        </script>
    

    
</div>







    
      <div class="duoshuo" id="comments">
    <div id="comment-box" ></div>
    <div class="ds-thread" id="ds-thread" data-thread-key="posts/Matlab/matlab cpp混编/" data-title="matlab cpp混编" data-url="http://markma.tk/posts/Matlab/matlab cpp混编/"></div>
    <script>
        var duoshuoQuery = {short_name:"markma"};
        var loadComment = function(){
            var d = document, s = d.createElement('script');
            s.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
            s.async = true; s.charset = 'UTF-8';
            (d.head || d.body).appendChild(s);
        }

        
    </script>
    
    <script> loadComment(); </script>

</div>
    




    <div class="scroll" id="post-nav-button">
        
            <a href="/" title="Back to Homepage"><i class="fa fa-home"></i></a>
        

        <a title="Mini Archives"><i class="fa fa-bars"></i><i class="fa fa-times"></i></a>

        
            <a href="/posts/QtStudy/Cpp中回调函数与静态成员/" title="Next: Cpp中回调函数与静态成员">
                <i class="fa fa-angle-right"></i>
            </a>
        
    </div>

    <ul class="post-list"><li class="post-list-item"><a class="post-list-link" href="/posts/Matlab/matlab cpp混编/">matlab cpp混编</a></li><li class="post-list-item"><a class="post-list-link" href="/posts/QtStudy/Cpp中回调函数与静态成员/">Cpp中回调函数与静态成员</a></li><li class="post-list-item"><a class="post-list-link" href="/posts/QtStudy/Qt中pro文件写法/">Qt pro文件写法</a></li><li class="post-list-item"><a class="post-list-link" href="/posts/OpenSource/Qt串口开发QextserialPort/">Qt串口开发QextserialPort</a></li><li class="post-list-item"><a class="post-list-link" href="/posts/QtStudy/Qt常见错误汇总/">Qt 常见错误汇总</a></li><li class="post-list-item"><a class="post-list-link" href="/posts/OpenSource/Qt绘图库QCustomplot/">Qt绘图库 QCustomPlot</a></li><li class="post-list-item"><a class="post-list-link" href="/posts/OpenSource/cpp机器人库robot-library/">cpp机器人库robot-library</a></li><li class="post-list-item"><a class="post-list-link" href="/posts/OpenSource/开源库大全/">开源库大全</a></li><li class="post-list-item"><a class="post-list-link" href="/posts/Python/Python-入门学习（二）/">Python-入门学习（二）</a></li><li class="post-list-item"><a class="post-list-link" href="/posts/Python/Python-入门学习（一）/">Python-入门学习（一）</a></li><li class="post-list-item"><a class="post-list-link" href="/posts/QtStudy/Qt中界面类名的修改/">Qt ui界面调用</a></li><li class="post-list-item"><a class="post-list-link" href="/posts/Hexo/HEXO-Github-快速搭建属于自己的博客/">HEXO+Github Create your Blog</a></li><li class="post-list-item"><a class="post-list-link" href="/posts/Journal/hello-world/">Hello World</a></li></ul>




    <script>
        
    </script>
</div>
      <footer id="footer">
    <div class="outer">
        <div id="footer-info">
            <div class="footer-left">
                <i class="fa fa-copyright"></i> 
                2016-2017 Mark
            </div>
            <div class="footer-right">
                <a href="http://hexo.io/" target="_blank" title="A fast, simple &amp; powerful blog framework">Hexo</a>  Theme <a href="https://github.com/MOxFIVE/hexo-theme-yelee" target="_blank" title="Another simple and elegant theme for Hexo  v3.5">Yelee</a> by MOxFIVE <i class="fa fa-heart animated infinite pulse"></i>
            </div>
        </div>
        
            <div class="visit">
                
                    <span id="busuanzi_container_site_pv" style='display:none'>
                        <span id="site-visit" title="Site Visitors"><i class="fa fa-user" aria-hidden="true"></i><span id="busuanzi_value_site_uv"></span>
                        </span>
                    </span>
                
                
                    <span>| </span>
                
                
                    <span id="busuanzi_container_page_pv" style='display:none'>
                        <span id="page-visit"  title="Page Hits"><i class="fa fa-eye animated infinite pulse" aria-hidden="true"></i><span id="busuanzi_value_page_pv"></span>
                        </span>
                    </span>
                
            </div>
        
    </div>
</footer>
    </div>
    
<script data-main="/js/main.js" src="//cdn.bootcss.com/require.js/2.2.0/require.min.js"></script>

    <script>
        $(document).ready(function() {
            var iPad = window.navigator.userAgent.indexOf('iPad');
            if (iPad > -1 || $(".left-col").css("display") === "none") {
                var bgColorList = ["#9db3f4", "#414141", "#e5a859", "#f5dfc6", "#c084a0", "#847e72", "#cd8390", "#996731"];
                var bgColor = Math.ceil(Math.random() * (bgColorList.length - 1));
                $("body").css({"background-color": bgColorList[bgColor], "background-size": "cover"});
            }
            else {
                var backgroundnum = 27;
                var backgroundimg = "url(/background/bg-x.jpg)".replace(/x/gi, Math.ceil(Math.random() * backgroundnum));
                $("body").css({"background": backgroundimg, "background-attachment": "fixed", "background-size": "cover"});
            }
        })
    </script>





    <script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    }
});

MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';                 
    }       
});
</script>

<script src="//cdn.bootcss.com/mathjax/2.6.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


<div class="scroll" id="scroll">
    <a href="#" title="Back to Top"><i class="fa fa-arrow-up"></i></a>
    <a href="#comments" onclick="load$hide();" title="Comments"><i class="fa fa-comments-o"></i></a>
    <a href="#footer" title="Go to Bottom"><i class="fa fa-arrow-down"></i></a>
</div>
<script>
    // Open in New Window
    
        var oOpenInNew = {
            
            
            
            
            
            
             archives: ".archive-article-title", 
             miniArchives: "a.post-list-link", 
            
             friends: "#js-friends a", 
             socail: ".social a" 
        }
        for (var x in oOpenInNew) {
            $(oOpenInNew[x]).attr("target", "_blank");
        }
    
</script>

    <script>
        var originTitle = document.title;
        var titleTime;
        document.addEventListener("visibilitychange", function() {
            if (document.hidden) {
                document.title = "(つェ⊂) 我藏好了哦~ " + originTitle;
                clearTimeout(titleTime);
            }
            else {
                document.title = "(*´∇｀*) 被你发现啦~ " + originTitle;
                titleTime = setTimeout(function() {
                    document.title = originTitle;
                }, 2000);
            }
        })
    </script>

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>
  </div>
</body>
</html>